/**
 * @author {Yuntao Zhou}
 * depend ui/uitl.js
 * depend ui/scrollpanel.js
 * depend ui/tabemenu.js
 * depend core
 */
 
 if(!QSAPI){
	var QSAPI={};
}
if(!QSAPI.Widget){
	QSAPI.Widget={};
}

if(!QS_Lang){
	var QS_Lang={};
}

if(!QS_Lang.quicktake){
	QS_Lang.quicktake={};
}


QSAPI.Widget.Quicktake=function(container,config,callback,id){
	this.container=typeof container=='string'?$('#'+container):container;
	
	this.config={
		width:0,
		height:0,
		ticker:'0P000002RH',
		lang:'en_us',
		showAutoComplete:true,
		frequency:5000
	};
	if(config){
		$.extend(this.config,config);
	}
	
	this.callback={};
	if(callback){
		$.extend(this.callback,callback);
	}
	
	this.id=this.subscribeID=id;
	
	this.el=null;
	this.$head=null;
	this.$iframe=null;
	
	this.tickerObj=null;
	this.headHeight=0;
	this.url=QSAPI.getQSPath()+"apis/quicktakesdk.jsp?id="+this.id+"&version="+QSAPI.getQSVersion()+"&instid="+QSAPI.getInstID()+"&v=1.3";
	
	this.HTML='<div class="qs-quicktake">' +
		'<iframe id="qs-quicktake-iframe-'+this.id+'" class="qs-quicktake-iframe" frameborder="0" scrolling="no" src="'+(this.url+'&action=init'+this._buildParameters())+'"></iframe>'+
	'</div>';
	
	this.init();

};

QSAPI.Widget.Quicktake.prototype={
	init:function(){
		this.el=$(this.HTML).appendTo(this.container);
		this._initSize(this.config.width,this.config.height);
		this.$iframe=this.el.find('.qs-quicktake-iframe').css({ width:this.config.width + "px",height:this.config.height+"px"});
	},
	acceptTicker:function(tickerObj){
		if(tickerObj && typeof tickerObj.getField=='function'){
			var url=this.url+"&ticker="+(tickerObj.getField('secId')||tickerObj.getField('symbol'))+"&action=changeticker&showAutoComplete="+this.config.showAutoComplete;
			this.$iframe[0].src=url;
		}
	},
	changeLanguage:function(language){
		if(typeof language!='undefined' && language!=''){
			language=language.toLowerCase();
			if(language!=this.config.language){
				this.config.lang=language;
				var url=this.url+"&lang="+language+"&action=changelang&showAutoComplete="+this.config.showAutoComplete;
				this.$iframe[0].src=url;
			}
		}
	},
	_initSize:function(w,h){
		var parent = this.el.parent();		
        w = w>0?w:parent.width();
        h = h>0?h:parent.height();
		var flag=false;
		if(w!=this.config.width){
			this.config.width=w;
			flag=true;
		}
		if(h!=this.config.height){
			this.config.height=h;
			flag=true;
		}
		return flag;
	},
	changeOutSize:function(w,h){
		if(this.$iframe&&this._initSize(w,h)){
            this.$iframe.css({ width: w + "px",height:h+"px"});
			var url=this.url+"&lang="+this.config.lang+"&action=changesize&showAutoComplete="+this.config.showAutoComplete;	
			this.$iframe[0].src=url;
	   }
	},
	_buildParameters:function(){
		var paras=[];
		for(var k in this.config){
			if(k=='width'||k=='height'){
				continue;
			}
			paras.push("&"+k+"="+this.config[k]);
		}
		return paras.join('');
	}
};